var express = require('express');
var router = express.Router();
let request = require("request");
let $ = require("cheerio");



/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});
//获取歌手列表
router.get('/getPopSingerList',function(req, res, next) {
  let page = req.query.pageNum;
  request.get({url:`https://www.9ku.com/geshou/all-all-liuxing/${page}.htm`},function (error, response, body){
    let list = $(body).filter("li");
    let singerList = [];
    for(let i = 0;i<list.length;i++){
      singerList.push({
        link:"https://www.9ku.com"+$(list).eq(i).find(".t-i").attr("href"),
        img:$(list).eq(i).find("img").attr("src"),
        name:$(list).eq(i).find(".t-t").text()
      })
    }
    res.send(singerList);
  })
  
});
//获取歌手信息
router.get('/getSinger',function(req, res, next){
  let url = req.query.url;
  let photo = "",
      info = "",
      name = "",
      hot = "",
      songsList = [];

  request.get({url:`https://www.9ku.com${url}`},function (error, response, body){
    photo = $(body).find(".singerTop .t-i img").attr("src");
    info = $(body).find(".singerTop .jianjieAll").text();
    name = $(body).find(".singerInfo .t-t h1").text();
    hot = $(body).find(".singerInfo .t-t .redu>em").text();
    let domList = $(body).find(".singerMain ol>li");
    for(let i=0;i<domList.length;i++){
      songsList.push({
        songId:$(domList).eq(i).find("input").attr("value").slice(0,-1),
        songName:$(domList).eq(i).find(".songName a font").text()||$(domList).eq(i).find(".songName a").text()
      })
    }
    res.send({
      photo:photo,
      info:info,
      hot:hot,
      name:name,
      songsList:songsList
    });
  })
  
})
//通过歌曲id获取歌曲地址
// $downurl=baseurl+"/html/playjs/geshou/"+(Math.floor(dataid/1000)+1)+"/"+dataid+".js";
router.get('/getSongsUrl',function(req, res, next){
  let songId = req.query.songId;
  let url = 'https://www.9ku.com'+"/html/playjs/"+(Math.floor(songId/1000)+1)+"/"+songId+".js";
  console.log(url)
  request.get({url:url},function (error, response, body){
    let data = body.toString().slice(1,-1);
    data = JSON.parse(data);
    res.send(data)
  })
})

//排行榜-获取排行榜列表
router.get('/getNewSongsList',function(req, res, next){
  // https://www.9ku.com/music/
  let bigBangList = [];
  let otherBangList = [];
  request.get({url:'https://www.9ku.com/music/'},function (error, response, body){
    let list = $(body).find(".bangNavBd .bangNavList").eq(1).find("li");
    let o_list = $(body).find(".bangNavBd .bangNavList").eq(2).find("li");
    for(let i=0;i<list.length;i++){
      bigBangList.push({
        name:$(list).eq(i).find("a").text(),
        url:$(list).eq(i).find("a").attr("href")
      })
    }
    for(let i=0;i<o_list.length;i++){
      otherBangList.push({
        name:$(o_list).eq(i).find("a").text(),
        url:$(o_list).eq(i).find("a").attr("href")
      })
    }
    res.send({
      bigBangList:bigBangList,
      otherBangList:otherBangList
    })
  })
})

//获取排行榜歌曲列表
router.get('/getBangSongList',function(req, res, next){
  let url = req.query.url;
  // https://www.9ku.com/music/bdhot.htm
  let musicList = [];
  request.get({url:`https://www.9ku.com${url}`},function (error, response, body){
    let list = $(body).find(".bangMusic .songList ol li");
    for(let i=0;i<list.length;i++){
      musicList.push({
        songId:$(list).eq(i).find("input").attr("value").slice(0,-1),
        songName:$(list).eq(i).find(".songName").text()
      })
    }
    res.send(musicList);
  })
  
})

function getList(reqList){
  let list = [];
  for(let i=0;i<reqList.length;i++){
    list.push({
      tagName:$(reqList).eq(i).find("a").text(),
      url:$(reqList).eq(i).find("a").attr("href")
    })
  }
  return list
}

//获取tag
router.get('/getTagList',function(req,res,next){
  request.get({url:`https://www.9ku.com/zhuanji/taste.htm`},function (error, response, body){
    let list_1 = $(body).find(".category_list").eq(0).find("ul li");
    let list_2 = $(body).find(".category_list").eq(1).find("ul li");
    let list_3 = $(body).find(".category_list").eq(2).find("ul li");
    let list_4 = $(body).find(".category_list").eq(3).find("ul li");
    let list_5 = $(body).find(".category_list").eq(4).find("ul li");
    let list_6 = $(body).find(".category_list").eq(5).find("ul li");
    let xinqing = getList(list_1),changhe = getList(list_2),qufeng = getList(list_3),chun = getList(list_4),diyu = getList(list_5),niandai = getList(list_6);
    res.send({
      xinqing:xinqing,
      changhe:changhe,
      qufeng:qufeng,
      chun:chun,
      diyu:diyu,
      niandai:niandai
    })
  })
})

//通过tag获取音乐列表
router.get('/geSongListByTag',function(req,res,next){
  let url = req.query.url;
  let musicList = [];
  request.get({url:`https://www.9ku.com${url}`},function (error, response, body){
    let list = $(body).find(".intro_item>ol>li");
    for(let i=0;i<list.length;i++){
      musicList.push({
        songId:$(list).eq(i).find("input").attr("value").slice(0,-1),
        songName:$(list).eq(i).find("a").eq(0).text()
      })
    }
    res.send(musicList)
  })
})

//搜索
router.get('/search',function(req,res,next){
  let keyword = req.query.keyword;
  let url = encodeURI('https://baidu.9ku.com/song/?key='+keyword);
  let musicList = [];
  request.get({url:url},function (error, response, body){
    let list = $(body).find("#searchInfo .songList li");
    for(let i=0;i<list.length;i++){
      musicList.push({
        songId:$(list).eq(i).find("input").attr("value").slice(0,-1),
        songName:$(list).eq(i).find(".songName").text(),
        singerName:$(list).eq(i).find(".singerName").text().trim(),
        zjName:$(list).eq(i).find(".albumName").text()
      })
    }
    res.send(musicList);
  })
})




module.exports = router;
